from flask import Flask, request, jsonify
from pandas import DataFrame
import joblib
import json

# 加载模型
model = joblib.load("test.pkl")

# 创建flask对象
app = Flask(__name__)


@app.route("/pred", methods=["POST"])
def pred():
    if request.method == "POST":
        # 字符串转字典
        content = json.loads(request.json)
        # 转成DF
        content = DataFrame(content, index=[0])
        # 预测
        pred = model.predict(content)

        # 返回json数据
        response = {
            "code": 0,
            "msg": "ok",
            "result": pred[0]
        }
        return jsonify(response)


if __name__ == '__main__':
    app.run()
